package com.my.kq.controller;

import com.my.kq.entity.Employee;
import com.my.kq.service.EmployeeService;
import com.my.kq.service.attendanceService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@WebServlet("/attendanceadd")
public class attendanceadd extends HttpServlet {
    private attendanceService attendanceService=new attendanceService();
    private EmployeeService EmployeeService=new EmployeeService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            HttpSession session = req.getSession();
            Employee cook =(Employee) session.getAttribute("cook");
            Integer id = cook.getId();
            Employee yee= EmployeeService.Empcx(id);
            //员工编号
            Integer number = yee.getDepartment_number();



             //日期
            LocalDate now1 = LocalDate.now();
            //时间
            LocalTime now2 = LocalTime.now();
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String day = df.format(now1);
            DateTimeFormatter df2 = DateTimeFormatter.ofPattern("HH:mm:ss");
            String startTime = df2.format(now2);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");

            String[] split = startTime.split(":");
            //打卡时间
            String timeType="";
            if(Integer.parseInt(split[0])<12){
                timeType="上午";
            }else {
                timeType="下午";
            }
            //打卡状态
            String starTime="";
            if(Integer.parseInt(split[0])>=8&&Integer.parseInt(split[0])<9){
                starTime="正常";
                session.setAttribute("str",starTime);
            }
            if(Integer.parseInt(split[0])>=9&&Integer.parseInt(split[0])<10){
                starTime="迟到";
                session.setAttribute("str",starTime);
            }
            //判断是否传输
           if(Integer.parseInt(split[0])>=8&&Integer.parseInt(split[0])<=10){

            attendanceService.attenadd(number,day,timeType,startTime,starTime);
           }



            req.getRequestDispatcher("jsp/welcome.jsp").forward(req,resp);


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
